#ifndef WEBSOCKETSERVER_H
#define WEBSOCKETSERVER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QTimer>
#include <QImage>
#include <QJsonObject>
#include <QJsonDocument>
#include <QByteArray>
#include <QHash>
#include <QMutex>
#include <QThread>
#include <QQueue>

class WebSocketServer : public QObject
{
    Q_OBJECT

public:
    explicit WebSocketServer(QObject *parent = nullptr);
    ~WebSocketServer();

    bool startServer(quint16 port = 8765);
    void stopServer();
    bool isServerRunning() const;
    int getClientCount() const;

    // 广播功能
    void broadcastFrame(const QImage &image);
    void broadcastFrameAsync(const QImage &image); // 异步版本，避免阻塞主线程
    void broadcastAudio(const QByteArray &audioData);
    void broadcastMessage(const QJsonObject &message);
    
    // 设置功能
    void setTargetFps(int fps);

    // 应用程序管理
    void setApplicationManager(class ApplicationManager *manager);

signals:
    void clientConnected(QTcpSocket *client);
    void clientDisconnected(QTcpSocket *client);
    void messageReceived(QTcpSocket *client, const QJsonObject &message);

private slots:
    void onNewConnection();
    void onClientDisconnected();
    void onClientDataReady();
    void onApplicationListTimer();

private:
    // WebSocket协议处理
    bool performHandshake(QTcpSocket *client, const QByteArray &requestData);
    void handleWebSocketFrame(QTcpSocket *client, const QByteArray &data);
    void sendWebSocketFrame(QTcpSocket *client, const QByteArray &data, quint8 opcode = 0x81);
    void sendTextMessage(QTcpSocket *client, const QJsonObject &message);
    void sendBinaryMessage(QTcpSocket *client, const QByteArray &data);
    
    // 消息处理
    void handleClientMessage(QTcpSocket *client, const QJsonObject &message);
    void handlePing(QTcpSocket *client);
    void handlePong(QTcpSocket *client);
    void handleFrameRequest(QTcpSocket *client);
    void handleAudioRequest(QTcpSocket *client);
    void handleLaunchApp(QTcpSocket *client, const QJsonObject &message);
    
    // 图像编码
    QByteArray encodeImageToBase64(const QImage &image, int quality = 80);
    QJsonObject createFrameMessage(const QImage &image, qint64 timestamp = 0, int frameIndex = 0);
    QJsonObject createAudioMessage(const QByteArray &audioData, const QString &format = "pcm");
    
    // 工具函数
    QByteArray generateWebSocketKey(const QByteArray &key);
    QByteArray maskData(const QByteArray &data, const QByteArray &mask);
    QByteArray unmaskData(const QByteArray &data, const QByteArray &mask);

private:
    QTcpServer *m_server;
    QList<QTcpSocket*> m_clients;
    QMutex m_clientsMutex;
    
    // 客户端数据缓冲区
    QHash<QTcpSocket*, QByteArray> m_clientBuffers;
    QMutex m_buffersMutex;
    
    // 应用程序管理
    class ApplicationManager *m_applicationManager;
    QTimer *m_applicationListTimer;
    
    // 状态
    bool m_isRunning;
    int m_frameIndex;
    
    // 帧率控制
    qint64 m_lastFrameTime;
    int m_targetFps;
    qint64 m_frameIntervalMs;
};

#endif // WEBSOCKETSERVER_H
